/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.action;

import net.cellobject.javacodeformatter.view.FormatterViewNameFilter;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IMemento;

/**
 * This action prompts the user for a string with wildcards used to filter the favorites view. Any favorite item with a name
 * matching the specified string will be filtered from the Favorites view.
 * <p>
 * This could (should?) be implemented as a subclass of {@link ContributionItem} but instead we subclass Action to illustrate the
 * older approach.
 */
public class FormatterViewFilterAction extends Action {
	private final Shell shell;

	private final FormatterViewNameFilter nameFilter;

	public FormatterViewFilterAction(final StructuredViewer viewer, final String text) {
		super(text);
		shell = viewer.getControl().getShell();
		nameFilter = new FormatterViewNameFilter(viewer);
	}

	@Override
	public void run() {
		final InputDialog dialog = new InputDialog(shell, "Favorites View Filter", "Enter a name filter pattern" + " (* = any string, ? = any character)"
				+ System.getProperty("line.separator") + "or an empty string for no filtering:", nameFilter.getPattern(), null);
		if (dialog.open() == InputDialog.OK) {
			nameFilter.setPattern(dialog.getValue().trim());
		}
	}

	public void saveState(final IMemento memento) {
		nameFilter.saveState(memento);
	}

	public void init(final IMemento memento) {
		nameFilter.init(memento);
	}
}
